package org.example.HttpClient.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.example.HttpClient.HttpInterface.HttpController;
import org.example.HttpClient.Pojo.AxiosConfig;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

public abstract class JAxiosConfig  implements HttpController {
    public AxiosConfig axiosConfig;
    public HttpController setHeader(Map<String, String> header) {
        axiosConfig.getHeader().putAll(header);
        return this;
    }

    public HttpController setHeader(Object header) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        axiosConfig.getHeader().putAll(BeanUtils.describe(header));
        return this;
    }


    public HttpController setParameter(Map<String, String> parameter) {
        axiosConfig.getHeader().putAll(parameter);
        return this;

    }

    public HttpController setParameter(Object parameter) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        axiosConfig.getHeader().putAll(BeanUtils.describe(parameter));
        return this;
    }

    public HttpController setJsonBody(String key, Object Data) throws JsonProcessingException {
        axiosConfig.getHeader().put("Content-Type", "application/json");
        // 创建JSON对象
        ObjectMapper mapper = new ObjectMapper();
        // 对象转JSON字符串
        String json = mapper.writeValueAsString(Data);
        // 创建NameValuePair对象
        axiosConfig.getData().add(new BasicNameValuePair(key, json));

        return this;
    }
    public HttpController setBody(List<? extends NameValuePair> Data) {
        axiosConfig.getData().addAll(Data);
        return this;
    }
}
